package com.ycl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.common.CustomException;
import com.ycl.common.Result;
import com.ycl.entity.Category;
import com.ycl.entity.Dish;
import com.ycl.entity.Setmeal;
import com.ycl.mapper.CategoryMapper;
import com.ycl.service.CategoryService;
import com.ycl.service.DishService;
import com.ycl.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件，根据分类Id进行查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        long dish_Count = dishService.count(dishLambdaQueryWrapper);

        // 查询当前分类是否关联了菜品，如已关联，则抛出异常
        if(dish_Count > 0){
            // 已关联菜品，抛出业务异常
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件，根据分类Id进行查询
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        long setmeal_Count = setmealService.count(setmealLambdaQueryWrapper);

        // 查询当前分类是否关联了菜品，如已关联，则抛出异常
        if(setmeal_Count > 0){
            // 已关联套餐，抛出业务异常
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }
        super.removeById(id);

    }
}
